

*set filepath. NOTE: you will need to update the filepath to run this file
cd "/Users/jeremyhorowitz/Documents/Book/Replication files/Chapter 6"


**************************************************************
********** Table 6.1 *****************************************
**************************************************************

clear
use "Sept2007survey.dta"

recode vote_choice (1=1) (2/3=0) (else=.), gen(incumbent_voter)
recode vote_choice (2/3=1) (1=0) (else=.), gen(opposition_voter)

gen num=_n in 1/13
gen sat=. in 1/13
gen sat_inc=. in 1/13
gen sat_opp=. in 1/13

recode q35b1 (1=0) (2=1) (5/6=.), gen(sat_corr)
summ sat_corr
replace sat=r(mean) if _n==1
sum sat_corr if incumbent_voter==1
replace sat_inc=r(mean) if _n==1
sum sat_corr if opposition_voter==1
replace sat_opp=r(mean) if _n==1

recode q35b2 (1=0) (2=1) (5/6=.), gen(sat_econ)
summ sat_econ
replace sat=r(mean) if _n==2
sum sat_econ if incumbent_voter==1
replace sat_inc=r(mean) if _n==2
sum sat_econ if opposition_voter==1
replace sat_opp=r(mean) if _n==2

recode q35b3 (1=0) (2=1) (5/6=.), gen(sat_crime)
summ sat_crime
replace sat=r(mean) if _n==3
sum sat_crime if incumbent_voter==1
replace sat_inc=r(mean) if _n==3
sum sat_crime if opposition_voter==1
replace sat_opp=r(mean) if _n==3

recode q35b4 (1=0) (2=1) (5/6=.), gen(sat_ed)
summ sat_ed
replace sat=r(mean) if _n==4
sum sat_ed if incumbent_voter==1
replace sat_inc=r(mean) if _n==4
sum sat_ed if opposition_voter==1
replace sat_opp=r(mean) if _n==4

recode q35b5 (1=0) (2=1) (5/6=.), gen(sat_health)
summ sat_health
replace sat=r(mean) if _n==5
sum sat_health if incumbent_voter==1
replace sat_inc=r(mean) if _n==5
sum sat_health if opposition_voter==1
replace sat_opp=r(mean) if _n==5

recode q35b6 (1=0) (2=1) (5/6=.), gen(sat_unemp)
summ sat_unemp
replace sat=r(mean) if _n==6
sum sat_unemp if incumbent_voter==1
replace sat_inc=r(mean) if _n==6
sum sat_unemp if opposition_voter==1
replace sat_opp=r(mean) if _n==6

recode q35b7 (1=0) (2=1) (5/6=.), gen(sat_ag)
summ sat_ag
replace sat=r(mean) if _n==7
sum sat_ag if incumbent_voter==1
replace sat_inc=r(mean) if _n==7
sum sat_ag if opposition_voter==1
replace sat_opp=r(mean) if _n==7

recode q35b8 (1=0) (2=1) (5/6=.), gen(sat_trans)
summ sat_trans
replace sat=r(mean) if _n==8
sum sat_trans if incumbent_voter==1
replace sat_inc=r(mean) if _n==8
sum sat_trans if opposition_voter==1
replace sat_opp=r(mean) if _n==8

recode q35b9 (1=0) (2=1) (5/6=.), gen(sat_land)
summ sat_land
replace sat=r(mean) if _n==9
sum sat_land if incumbent_voter==1
replace sat_inc=r(mean) if _n==9
sum sat_land if opposition_voter==1
replace sat_opp=r(mean) if _n==9

recode q35b10 (1=0) (2=1) (5/6=.), gen(sat_pris)
summ sat_pris
replace sat=r(mean) if _n==10
sum sat_pris if incumbent_voter==1
replace sat_inc=r(mean) if _n==10
sum sat_pris if opposition_voter==1
replace sat_opp=r(mean) if _n==10

recode q35b11 (1=0) (2=1) (5/6=.), gen(sat_const)
summ sat_const
replace sat=r(mean) if _n==11
sum sat_const if incumbent_voter==1
replace sat_inc=r(mean) if _n==11
sum sat_const if opposition_voter==1
replace sat_opp=r(mean) if _n==11

recode q35b13 (1=0) (2=1) (5/6=.), gen(sat_housing)
summ sat_housing
replace sat=r(mean) if _n==12
sum sat_housing if incumbent_voter==1
replace sat_inc=r(mean) if _n==12
sum sat_housing if opposition_voter==1
replace sat_opp=r(mean) if _n==12

recode q35b14 (1=0) (2=1) (5/6=.), gen(sat_terrorism)
summ sat_terrorism
replace sat=r(mean) if _n==13
sum sat_terrorism if incumbent_voter==1
replace sat_inc=r(mean) if _n==13
sum sat_terrorism if opposition_voter==1
replace sat_opp=r(mean) if _n==13

gsort -sat
list num sat sat_inc sat_opp if _n<14



**************************************************************
********** Table 6.2 *****************************************
**************************************************************

clear
use "Sept2007survey.dta"

gen ed_kept=0
replace ed_kept=1 if q39n1==1 | q39n2==1 | q39n3==1

gen goodgov_kept=0
replace goodgov_kept=1 if q39n1==5 | q39n2==5 | q39n3==5

gen impecon_kept=0
replace impecon_kept=1 if q39n1==3 | q39n2==3 | q39n3==3

gen infra_kept=0
replace infra_kept=1 if q39n1==8 | q39n2==8 | q39n3==8

gen health_kept=0
replace health_kept=1 if q39n1==7 | q39n2==7 | q39n3==7

gen cdf_kept=0
replace cdf_kept=1 if q39n1==10 | q39n2==1 | q39n3==10

tab ed_kept if q38==3 | q38==4
tab cdf_kept if q38==3 | q38==4
tab impecon_kept if q38==3 | q38==4
tab infra_kept if q38==3 | q38==4
tab health_kept if q38==3 | q38==4
tab goodgov_kept if q38==3 | q38==4




**************************************************************
********** Figure 6.1 ****************************************
**************************************************************

use "appeals2007.dta", clear

keep if party=="PNU"

gen pos_sect_simp2 = pos_sect_simplified
replace pos_sect_simp2 = "Education" if pos_sect_simp2=="ed"
replace pos_sect_simp2 = "Infrastructure" if pos_sect_simp2=="infra"
replace pos_sect_simp2 = "Econ/jobs/poverty" if pos_sect_simp2=="econ/jobs/pov"
replace pos_sect_simp2 = "Gen Development" if pos_sect_simp2=="gen"
replace pos_sect_simp2 = "Gen Representation" if pos_sect_simp2=="genrep"
replace pos_sect_simp2 = "Health" if pos_sect_simp2=="health"
replace pos_sect_simp2 = "District creation" if pos_sect_simp2=="dist"
replace pos_sect_simp2 = "Women's employment" if pos_sect_simp2=="women/jobs"

replace pos_sect_simp2 = "Other" if pos_sect_simp2=="lands" | pos_sect_simp2=="freedom" | pos_sect_simp2=="women" | pos_sect_simp2=="title deeds" ///
| pos_sect_simp2=="squatters" | pos_sect_simp2=="security" | pos_sect_simp2=="taxes" | pos_sect_simp2=="sec (anti-majimbo)" | ///
pos_sect_simp2=="sec" | pos_sect_simp2=="police" | pos_sect_simp2=="parking" | pos_sect_simp2=="orphans" | pos_sect_simp2=="market" | ///
pos_sect_simp2=="dump" | pos_sect_simp2=="disabled" | pos_sect_simp2=="dev" | pos_sect_simp2=="appts" | pos_sect_simp2=="LATF/CDF" | pos_sect_simp2=="perf" | ///
pos_sect_simp2=="youth" | pos_sect_simp2=="youth/jobs" | pos_sect_simp2=="cdf"


sort pos_sect_simp2 perf_prom
gen count=.
replace count=1 if pos_sect_simp2!=""
sort perf_prom pos_sect_simp2
egen total_pos=total(count) if count!=.
by perf_prom pos_sect_simp2: egen cat_total=total(count)
sort pos_sect_simp2
by pos_sect_simp2: egen cat_total_total=total(count)
gen cat_share_perf = cat_total/total_pos if perf_prom=="perf"
gen cat_share_prom = cat_total/total_pos if perf_prom=="prom"


graph bar cat_share_perf cat_share_prom, ///
over(pos_sect_simp2, label(angle(45)) sort(cat_total_total) reverse) stack  ///
scheme(s1mono) ytitle("Percent") ylabel(.1 "10" .2 "20" .3 "30", nogrid angle(0)) plotregion(lcolor(white)) title(" ") bar(1, color(gs8) lcolor(black)) ///
bar(2, color(gs12) lcolor(black)) ///
legend(position(2) ring(0) bmargin(large) label(1 "Performance") label(2 "Promises") col(1) order(2 1) symys(*.6) symxs(*.4) size(medium)) ///
graphregion(margin(b=32)) scale(1.2) name(g1, replace)



**************************************************************
********** Figure 6.2 ****************************************
**************************************************************


*Top panel - (a) Kikuyu, Kalenjin, and Other Districts
use "enrollment_data.dta", clear

by district: replace kikuyu_share62=kikuyu_share62[_n-1] if kikuyu_share62==.
by district: replace kalenjin_share62=kalenjin_share62[_n-1] if kalenjin_share62==.
gen group = "other"
foreach X in kikuyu  kalenjin {
replace group = "`X'" if `X'_share >= .50
}
collapse (sum) primary_enrolment pop_interp primary_age_people_2, by(group year) cw
sort year group
drop if year<1970

gen primary_share = primary_enrolment / primary_age_people_2

replace group = "1. Kikuyu Districts" if group == "kikuyu"
replace group = "2. Kalenjin Districts" if group == "kalenjin"
replace group = "3. Other Districts" if group == "other"


twoway (line primary_share year if group == "1. Kikuyu Districts", lpattern(solid)) ///
(line primary_share year if group == "2. Kalenjin Districts", lpattern(shortdash)) ///
(line primary_share year if group == "3. Other Districts", lpattern(longdash) lcolor(gs5)), ///
ytitle(Gross Enrollment Rate, size(medsmall)) ytitle(, margin(small)) xtitle(., size(zero)) yline(1, lpattern(solid) lcolor(gs10)) ///
legend(order(1 "Kikuyu Districts" 2 "Kalenjin Districts" 3 "Other Districts") rows(1) span) xlabel(#5) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xlabel(1970 "1970" 1980 "1980" 1990 "1990" 2000 "2000" 2010 "2010" 2020 " ") ylabel(0(.2)1.3, nogrid angle(0)) ///
xline(1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992, lwidth(vvthick) lcolor(gs15)) ///
xline(1978.67, lpattern(solid)) xline(2002, lpattern(solid)) xline(2013, lpattern(solid)) /// 
text(1.34 1973 "J. Kenyatta", place(n) nobox size(small)) text(1.29 1973 "(Kikuyu)", place(n) nobox size(small)) ///
text(1.34 1991 "Moi", place(n) nobox size(small)) text(1.29 1991 "(Kalenjin)", place(n) nobox size(small)) ///
text(1.34 2008 "Kibaki", place(n) nobox size(small)) text(1.29 2008 "(Kikuyu)", place(n) nobox size(small)) ///
text(1.34 2017 "U. Kenyatta", place(n) nobox size(small)) text(1.29 2017 "(Kikuyu)", place(n) nobox size(small)) name(g1, replace)


*Bottom panel - (b) Coethnic and Other Districts
use "enrollment_data.dta", clear

by district: replace kikuyu_share62=kikuyu_share62[_n-1] if kikuyu_share62==.
by district: replace kalenjin_share62=kalenjin_share62[_n-1] if kalenjin_share62==.

gen group="other"
replace group="pres_match" if (district_41 =="kiambu" | district_41 =="kirinyaga" | district_41 =="muranga" | district_41 =="nyandarua" | district_41 =="nyeri" | district_41 =="laikipia" | district_41=="nakuru") & (year<=1979 | year>=2003)
replace group="pres_match" if (district_41 =="baringo" | district_41 =="kericho" | district_41 =="elgeyo marakwet" | district_41 =="nandi" | district_41 =="uasin gishu" | district_41 =="west pokot") & (year>=1980 & year<=2002)

collapse (sum) primary_enrolment pop_interp primary_age_people_2, by(group year) cw
sort year group

gen primary_share = primary_enrolment / primary_age_people_2

twoway ///
(line primary_share year if group == "pres_match") ///
(line primary_share year if group == "other", lpattern(longdash) lcolor(gs5)), ///
ytitle(Gross Enrollment Rate, size(medsmall)) ytitle(, margin(small)) xtitle(., size(zero)) yline(1, lpattern(solid) lcolor(gs10)) ///
legend(order(1 "Coethnic districts" 2 "Other districts") rows(1)) xlabel(#5) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xlabel(1970 "1970" 1980 "1980" 1990 "1990" 2000 "2000" 2010 "2010" 2020 " ") ylabel(0(.2)1.3, nogrid angle(0)) ///
xline(1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992, lwidth(vvthick) lcolor(gs15)) ///
xline(1978.67, lpattern(solid) lwidth(thin)) xline(2002, lpattern(solid) lwidth(thin)) xline(2013, lpattern(solid) lwidth(thin)) /// 
text(1.34 1973 "J. Kenyatta", place(n) nobox size(small)) text(1.29 1973 "(Kikuyu)", place(n) nobox size(small)) ///
text(1.34 1991 "Moi", place(n) nobox size(small)) text(1.29 1991 "(Kalenjin)", place(n) nobox size(small)) ///
text(1.34 2008 "Kibaki", place(n) nobox size(small)) text(1.29 2008 "(Kikuyu)", place(n) nobox size(small)) ///
text(1.34 2017 "U. Kenyatta", place(n) nobox size(small)) text(1.29 2017 "(Kikuyu)", place(n) nobox size(small)) name(g2, replace)



**************************************************************
********** Table 6.2 *****************************************
**************************************************************

use "enrollment_data.dta", clear
gen pres_matchMP=pres_match*mp

gen primary_share = primary_enrolment / primary_age_people_2

*model 1
areg primary_share pres_match pres_matchMP mp i.year if year>1969, absorb(district) robust cluster(district)
lincom pres_match + pres_matchMP

*model 2
areg primary_share pres_match pres_matchMP mp pop1962_t area_t urbrate1962_t wage_employment_t value_cashcrops_t i.year if year>1969, absorb(district) robust cluster(district)
lincom pres_match + pres_matchMP 

*Model 3 
tsset dist_code year
xi: xtreg primary_share pres_match pres_matchMP mp i.dist_code*year if year>1969, robust cluster(dist_code) fe
lincom pres_match + pres_matchMP 



**************************************************************
********** Figure 6.3 ****************************************
**************************************************************

use "enrollment_data.dta", clear
by district: replace kikuyu_share62=kikuyu_share62[_n-1] if kikuyu_share62==.

gen group="other"
replace group="kikuyu" if (district_41 =="kiambu" | district_41 =="kirinyaga" | district_41 =="muranga" | district_41 =="nyandarua" | district_41 =="nyeri" | district_41 =="laikipia" | district_41=="nakuru") 

collapse (sum) primary_enrolment pop_interp primary_age_people_2, by(group year) cw
sort year group

gen primary_share = primary_enrolment / primary_age_people_2

keep if year>1991 & year<2014

twoway ///
(line primary_share year if group == "kikuyu", lcolor(black)) ///
(line primary_share year if group == "other", lpattern(longdash) lcolor(black)), ///
ytitle(Gross Enrollment Rate, size(medsmall)) ytitle(, margin(small)) xtitle(., size(zero)) yline(1, lpattern(solid) lcolor(black)) ///
legend(order(1 "Kikuyu districts" 2 "Other districts") rows(1)) xlabel(#5) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xlabel(1992 "1992" 1997 "1997" 2002 "2002" 2007 "2007" 2012 "2012") ylabel(0(.2)1.3, nogrid angle(0)) ///
xline(2002, lpattern(shortdash) lcolor(black)) 
graph export image4.pdf, replace




**************************************************************
********** Table 6.4 *****************************************
**************************************************************

use "enrollment_diffindiff.dta", clear

gen primary_share = primary_enrolment / primary_age_people_2

*model 1
reg primary_share free_primary kikuyu_majority freeXkikuyu if (year>=1998 & year<=2007), cluster(dist_code) robust
lincom free_primary + freeXkikuyu 

*model 2
reg primary_share free_primary kikuyu_majority freeXkikuyu schools_interp edu_alloc bursary_alloc wage_employment_interp earnings_interp luminosity pop_interp area pop_dense_interp asal trend if (year>=1998 & year<=2007), cluster(dist_code)
lincom free_primary + freeXkikuyu 

*random effects
xtset dist_code
xi: xtreg primary_share free_primary kikuyu_majority freeXkikuyu schools_interp edu_alloc bursary_alloc wage_employment_interp earnings_interp luminosity pop_interp area pop_dense_interp asal trend if (year>=1998 & year<=2007), cluster(dist_code) re
lincom free_primary + freeXkikuyu 



**************************************************************
********** Figure 6.4 ****************************************
**************************************************************

use "schools_final.dta", clear


*Top panel - (a) Kikuyu, Kalenjin, and Other Districts
preserve 
drop group
gen group = "other"
replace group = "kikuyu" if kikuyu>50
replace group = "kalenjin" if kalenjin>50
collapse (sum) population primaries_govt dispensaries_govt, by(year group) cw
order group
gen prim_pc=(primaries_govt/population)*1000
gen disp_pc=(dispensaries_govt/population)*1000

twoway (line prim_pc year if group == "kikuyu", lwidth(medthick) lpattern(shortdash)) (line prim_pc year if group == "kalenjin", lwidth(medthick) lpattern(solid)) ///
(line prim_pc year if group == "other", lcolor(gs5) lwidth(medthick) lpattern(longdash)), ytitle("Primary schools per 1,000", size(medsmall)) ytitle(, margin(small)) xtitle(., size(zero)) ///
legend(order(1 "Kikuyu Districts" 2 "Kalenjin Districts" 3 "Other Districts") rows(1) span) xlabel(#5) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xlabel(1974(5)2014) ///
ylabel(.0(.2)1.3, nogrid angle(0)) ///
xline(1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992, lwidth(vvvthick) lcolor(gs15)) ///
xline(1978.67, lpattern(solid) lwidth(thin)) xline(2002, lpattern(solid) lwidth(thin))  /// 
text(1.34 1976 "J. Kenyatta", place(n) nobox size(small)) text(1.34 1991 "Moi", place(n) nobox size(small)) ///
text(1.34 2008 "Kibaki", place(n) nobox size(small)) ///
text(1.29 1976 "(Kikuyu)", place(n) nobox size(small)) text(1.29 1991 "(Kalenjin)", place(n) nobox size(small)) ///
text(1.29 2008 "(Kikuyu)", place(n) nobox size(small)) 
restore


*Bottom panel - (b) Coethnic and Other Districts
drop group
gen group = "other"
replace group = "pres_match" if kikuyu>50 & (year<=1979 | year>=2003)
replace group = "pres_match" if kalenjin>50 &  (year>=1980 & year<=2002)

collapse (sum) population primaries_govt dispensaries_govt, by(year group) cw
order group
gen prim_pc=(primaries_govt/population)*1000
gen disp_pc=(dispensaries_govt/population)*1000

twoway (line prim_pc year if group == "pres_match", lwidth(medthick)) ///
(line prim_pc year if group == "other", lcolor(gs5) lwidth(medthick) lpattern(longdash)), ytitle("Primary schools per 1,000", size(medsmall)) ytitle(, margin(small)) xtitle(., size(zero)) ///
legend(order(1 "Coethnic Districts" 2 "Other Districts") rows(1) span) xlabel(#5) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xlabel(1974(5)2014) ///
ylabel(.0(.2)1.3, nogrid angle(0)) ///
xline(1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992, lwidth(vvvthick) lcolor(gs15)) ///
xline(1978.67, lpattern(solid) lwidth(thin)) xline(2002, lpattern(solid) lwidth(thin))  /// 
text(1.34 1976 "J. Kenyatta", place(n) nobox size(small)) text(1.34 1991 "Moi", place(n) nobox size(small)) ///
text(1.34 2008 "Kibaki", place(n) nobox size(small)) ///
text(1.29 1976 "(Kikuyu)", place(n) nobox size(small)) text(1.29 1991 "(Kalenjin)", place(n) nobox size(small)) ///
text(1.29 2008 "(Kikuyu)", place(n) nobox size(small)) 




**************************************************************
********** Table 6.5 *****************************************
**************************************************************

use "schools_final.dta", clear

areg prim_pc pres_match pres_matchMP multiparty i.year, vce(cluster dist_code) absorb(dist_code)
lincom pres_match + pres_matchMP 










